

$(function () {
    //1.抽取变量
    var customer_datagrid = $("#customer_datagrid");
    var customer_dialog = $("#customer_dialog");
    var customer_form = $("#customer_form");
    var cust_dialog = $("#cust_dialog");
    var cust_datagrid = $("#cust_datagrid");
    var mark_dialog = $("#mark_dialog");
    var mark_form = $("#mark_form");


    customer_datagrid.datagrid({
        url:"/customerPlan/list?status=1",
        fit: true,
        fitColumns: true,
        striped: true,
        toolbar: '#customer_toolbar',
        pagination: true,
        singleSelect: true,
        rownumbers:true,
        columns: [[
            {field:'id',title:'编号',width:30},
            {field:'result',title:'执行结果',width:30,formatter:function (value,row,index) {
                if (value == 3){
                    return "<span style='color: blue'>优</span>";
                };
                if (value == 2){
                    return "<span style='color: blue'>中</span>";
                };
                if (value == 1){
                    return "<span style='color: blue'>差</span>";
                };
            }},
            {field:'date',title:'计划时间',width:50},
            {field:'inputUser',title:'创建人',width:50,formatter:function (value,row,index) {
                return value ? value.realName : '';
            }},
            {field:'customer',title:'客户',width:30,formatter:function (value,row,index) {
                return value ? value.name : '';
            }},
            {field:'subject',title:'计划主题',width:50},
            {field:'type',title:'实施方式',width:50,formatter:function (value, row, index) {
                return value ? value.name : '';
            }},
            {field:'detail',title:'详情',width:80},
            {field:'remark',title:'备注',width:50},
        ]],
    })

    //模态框数据表格
    cust_datagrid.datagrid({
        url:"/customer/list?status=2",
        fit: true,
        fitColumns: true,
        striped: true,
        pagination: true,
        singleSelect: true,
        rownumbers:true,
        columns: [[
            {field:'chargeUser',title:'负责人',width:60,formatter:function (value,row,index) {
                return value ? value.realName : '';
            }},
            {field:'name',title:'客户姓名',width:70},
            {field:'age',title:'年龄',width:35},
            {field:'gender',title:'性别',width:35,formatter:function (value, row, index) {
                if (value){
                    return '男';
                }
                if (!value){
                    return '女';
                }
            }},
            {field:'job',title:'职业',width:100,formatter:function (value, row, index) {
                return value ? value.name : '';
            }},
            {field:'salaryLevel',title:'薪资水平',width:100,formatter:function (value, row, index) {
                return value ? value.name : '';
            }}
        ]],
    })





    var methodObj = {
        reload:function () {
            customer_datagrid.datagrid("reload");
        },
        cancel: function () {
            customer_dialog.dialog("close");
            mark_dialog.dialog("close")
            mark_form.dialog("close");
        },
        //删除
        remove:function () {
            var row = customer_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert('温馨提示', '请选中一条记录！', 'info');
                return;
            }

            $.messager.confirm("温馨提示", "确定要删除计划 : "+"<span style='color: red'>"+row.subject+"</span>"+" 吗?",function (yes) {
                if (yes){
                    $.get("/customerPlan/delete?id="+row.id,function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.message, "info", function () {
                                customer_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.message, "info");
                        }
                    })
                }
            })
        },
        add: function () {
            customer_form.form("clear");
            customer_dialog.dialog("setTitle", "新增记录");
            customer_dialog.dialog("open");
        },
        cust_btn:function () {
            cust_dialog.dialog("open")

        },
        //确定选择客户
        get_cust:function () {
            var row = cust_datagrid.datagrid("getSelected");

            $("[name='customer.id']").val(row.id);
            //setText text 设置显示的文本值
            $("#customerName").textbox("setText",row.name);
            cust_dialog.dialog("close");
        },
        //修改计划
        edit: function () {
            var row = customer_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert('温馨提示', '请选中一条记录！', 'info');
                return;
            }

            customer_form.form("clear");

            if (row.customer) {
                row["customer.id"] = row.customer.id;
                $("#customerName").textbox("setText",row.customer.name)
            }
            if (row.type) {
                row["type.name"] = row.type.name;
            }
            customer_form.form("load", row);
            customer_dialog.dialog("setTitle", "修改记录");
            customer_dialog.dialog("open");
        },
        save: function () {
            var id = $("#customer_form input[name='id']").val();
            var url;
            if (!id) {
                url = "/customerPlan/save";
            } else {

                url = "/customerPlan/edit"
            }

            customer_form.form("submit", {
                url: url,
                onSubmit:function (param) {
                    //在提交表单之间需要设置状态
                    param['status'] =1;

                },
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.message, "info", function () {
                            customer_dialog.dialog("close");
                            customer_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.message, "info");
                    }
                }
            })
        },

        //标记执行结果
        remark:function () {
            var row = customer_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert('温馨提示', '请选中一条记录！', 'info');
                return;
            }

            mark_form.form("clear");

            row["id"] = row.id;
            mark_form.form("load",row);
            $("#sn").html(row.id);
            $("#subject").html(row.subject);
            mark_form.dialog("open");
        },
        //标记结果提交
        result:function () {
            mark_form.form("submit", {
                url: "/customerPlan/result",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.message, "info", function () {
                            mark_form.dialog("close");
                            customer_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.message, "info");
                    }
                }
            })
        }

    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })




    //编辑或新增弹窗
    customer_dialog.dialog({
        width : 600,
        height : 370,
        buttons : "#customer_btns",
        closed : true
    });
    //弹窗表格选择
    cust_dialog.dialog({
        title:'请选择正式客户',
        width : 660,
        height : 390,
        buttons : "#get_btns",
        closed : true,
        //modal boolean 定义是否将窗体显示为模式化窗口
        modal:true,
    });
    //标记和备注弹窗
    mark_form.dialog({
        title:'标记执行结果',
        width : 300,
        height : 230,
        buttons : "#mark_btns",
        closed : true,
        modal:true,
    });

})

function mysearch() {
    var customer = $("input[name=customer]").val();
    var chargeUser = $("input[name=chargeUser]").val();
    var sbuject = $("input[name=sbuject]").val();
    var type = $("input[name=type]").val();
    var beginDate = $("input[name=beginDate]").val();
    var endDate = $("input[name=endDate]").val();

    $("#customer_datagrid").datagrid("load",{
        customer:customer,
        sbuject:sbuject,
        type:type,
        chargeUser:chargeUser,
        beginDate:beginDate,
        endDate:endDate
    })
}